﻿

using System;
using UnityEngine;

namespace Gameplay.CharacterFramework
{
    public class PhysicsComponent:CoreComponent
    {
        private Rigidbody _rb;
        public Rigidbody RB => _rb;

        private Vector3 _workspace;

        public Vector3 CurrentVelocity;

        protected override void Awake()
        {
            base.Awake();
            _rb = GetComponentInParent<Rigidbody>();
        }

        public override void PhysicsUpdate()
        {
            CurrentVelocity = _rb.velocity;
        }

        public void SetVelocityZero()
        {
            _rb.velocity = Vector2.zero;
            CurrentVelocity = Vector2.zero;
        }

        public void SetVelocity(Vector3 velocity)
        {
            _workspace.Set(velocity.x,velocity.y,velocity.z);
            _rb.velocity = _workspace;
            CurrentVelocity = _workspace;
        }
        public void SetVelocity(float veloctiyX,float velocityY,float velocityZ) 
        {
            _workspace.Set(veloctiyX,velocityY,velocityZ);
            _rb.velocity = _workspace;
            CurrentVelocity = _workspace;
        }

        public void SetVelocity(float velocity, Vector3 direction)
        {
            direction.Normalize();
            _workspace.Set(direction.x * velocity, direction.y * velocity,direction.z*velocity);
            _rb.velocity = _workspace;
            CurrentVelocity = _workspace;
        }

        public void SetVelocityX(float velocity)
        {
            _workspace.Set(velocity, CurrentVelocity.y,CurrentVelocity.z);
            _rb.velocity = _workspace;
            CurrentVelocity = _workspace;
        }

        public void SetVelocityY(float velocity)
        {
            _workspace.Set(CurrentVelocity.x, velocity, CurrentVelocity.z);
            _rb.velocity = _workspace;
            CurrentVelocity = _workspace;
        }

        public void AddForce(Vector3 force,ForceMode mode)
        {
            _rb.AddForce(force,mode);
        }
    }
}
